package com.zbkc.main;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.zbkc.bean.BeifenFileBean;
import com.zbkc.http.Config;
import com.zbkc.interfaces.SCInterface;
import com.zbkc.main.base.BaseTitleActivity;
import com.zbkc.util.AlertUtils;
import com.zbkc.util.BASE64Util;
import com.zbkc.util.StarCloudUtil;
import com.zbkc.util.ThreadPoolFactory;

public class BeifenFileActivity extends BaseTitleActivity {

	private ListView listView;

	private List<BeifenFileBean> fileBeanList;
	private String fileurl;
	private String result;

	private int position;

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:

				MyAdapter myAdapter = new MyAdapter(BeifenFileActivity.this, 0,
						fileBeanList);
				listView.setAdapter(myAdapter);
				break;
			case 2:
				Intent intent = StarCloudUtil.openFile(fileurl);
				startActivity(intent);
				break;
			}
			AlertUtils.dismissLoadingDialog();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_beifen_file);
		bindView();
		getData();
	}

	private void bindView() {
		setTitle("我的备份");
		setBtn2Visiable(false);

		listView = (ListView) findViewById(R.id.listview);
		listView.setOnItemClickListener(new onItemClick());
	}

	class onItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			position = arg2;
			downFile();
		}

	}

	private void getData() {
		final String str1 = getIntent().getStringExtra("id");
		final String str2 = getIntent().getStringExtra("a");
		final String str3 = getIntent().getStringExtra("b");
		final String str4 = getIntent().getStringExtra("c");
		final String str5 = getIntent().getStringExtra("d");
		AlertUtils.showLoadingDialog(this, "正在获取数据,请稍后...", true);
		ThreadPoolFactory.getInstance().execute(new Runnable() {
			public void run() {
				String str = BASE64Util.encode(str1.getBytes());
				fileBeanList = SCInterface.getBeifenFileList(str, str2, str3,
						str4, str5);
				handler.sendEmptyMessage(1);
			}
		});
	}

	private void downFile() {
		AlertUtils.showLoadingDialog(BeifenFileActivity.this, "正在打开文件,请稍后...",
				true);
		ThreadPoolFactory.getInstance().execute(new Runnable() {

			@Override
			public void run() {
				BeifenFileBean fileBean = fileBeanList.get(position);
				fileurl = Config.FILE_LOCATION + fileBean.getFileName();
				result = SCInterface.downFile(fileBean.getMake1(),
						fileBean.getMake2(), fileBean.getFileName());
				handler.sendEmptyMessage(2);
			}
		});
	}

	class MyAdapter extends ArrayAdapter<BeifenFileBean> {
		public MyAdapter(Context context, int textViewResourceId,
				List<BeifenFileBean> objects) {
			super(context, textViewResourceId, objects);
		}

		public View getView(int position, View view, ViewGroup paramViewGroup) {
			ViewHolder viewHolder;
			if (view == null) {
				viewHolder = new ViewHolder();
				view = View.inflate(getContext(),
						R.layout.listview_beifen_item, null);
				viewHolder.tvtitle = ((TextView) view
						.findViewById(R.id.tv_title));
				view.setTag(viewHolder);
			} else {
				viewHolder = ((ViewHolder) view.getTag());
			}
			viewHolder.tvtitle.setText(getItem(position).getFileName());
			return view;
		}

		class ViewHolder {
			TextView tvtitle;
		}
	}

}
